using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double sotruoc = 0;
        double sosau = 0;
        double bonho = 0;
        char pheptoan = ' ';
        double kq = 0;
    
        private void btnC_Click(object sender, EventArgs e)
        {
            txtNhap.Text = "";
            pheptoan = ' ';
        }
        private void btn7_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "7";
        }
        private void btn8_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "8";
        }
        private void btn9_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "9";
        }
        private void btn4_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "4";
        }
        private void btn5_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "5";
        }
        private void btn6_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "6";
        }
        private void btn1_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "1";
        }
        private void btn2_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "2";
        }
        private void btn3_Click(object sender, EventArgs e)
        {
            txtNhap.Text += "3";
        }
        private void NhanPhepToan()
        {
            if (txtNhap.Text.Trim() != "")
            sotruoc = Convert.ToDouble(txtNhap.Text.Trim());
            txtNhap.Text = "";
        }
        private void btnBang_Click(object sender, EventArgs e)
        {
            //Sua cai nay
            if (txtNhap.Text.Trim() == "")
                return;

            sosau = Convert.ToDouble(txtNhap.Text.Trim());
            switch (pheptoan)
            {
                case '+':
                    kq = sotruoc + sosau;
                    break;
                case '-':
                    kq = sotruoc - sosau;
                    break;
                case '*':
                    kq = sotruoc * sosau;
                    break;
                case '/':
                    kq = sotruoc / sosau;
                    break;
                case 'm':   //Mod
                    kq = sotruoc % sosau;
                    break;
                case 'y':   //Mu y
                    //double tam = sotruoc;
                    //for (int i = 0; i < sosau; i++)
                    //    tam = tam * sotruoc;
                    //kq = tam;
                    kq = Math.Pow(sotruoc, sosau);
                    break;
                case 'l':   //log
                    kq = Math.Log(sotruoc, sosau);
                    break;
            }
            txtNhap.Text = Convert.ToString(kq);
        }
        private void btnCong_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = '+';
        }
        private void btnBackspace_Click(object sender, EventArgs e)
        {
            	//tao sua cho nay nha
 	this.txtNhap.Text = this.txtNhap.Text.ToString().Remove(this.txtNhap.Text.Length - 1, 1);
        }
        private void btnChia_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = '/';
        }
        private void btnNhan_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = '*';

        }
        private void btnTru_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = '-';

        }
        private void btnCE_Click(object sender, EventArgs e)
        {
            txtNhap.Text = "";
        }
        private void btnMR_Click(object sender, EventArgs e)
        {
            txtNhap.Text = Convert.ToString(bonho);
        }
        private void btnM_Click(object sender, EventArgs e)
        {
            bonho = Convert.ToDouble(txtNhap.Text.Trim());
        }
        private void btnCham_Click(object sender, EventArgs e)
        {
            txtNhap.Text += ".";

        }
        private void btn0_Click(object sender, EventArgs e)
        {
            if(txtNhap.Text != "0")
                txtNhap.Text += "0";

        }
        private void btn1Chia_Click(object sender, EventArgs e)
        {
            double tam;
            tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = 1 / tam;
            txtNhap.Text = Convert.ToString(tam);
        }
        private void about_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog();
        }
        private void btnSqrt_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = Math.Sqrt(tam);
            txtNhap.Text = Convert.ToString(tam);

        }
        private void btnPhanTram_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = tam / 100;
            txtNhap.Text = Convert.ToString(tam);
        }
        private void msiStand_Click(object sender, EventArgs e)
        {
            this.Width = 320;
            grbExtend.Hide();
        }
        private void msiExtra_Click(object sender, EventArgs e)
        {
            this.Width = 520;
            grbExtend.Show();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            //this.Width = 320;
           // grbExtend.Hide();

        }
        private void btnMod_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = 'm'; //Mode

        }
  
        private void btmMu2_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = tam * tam;
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnMu3_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = tam * tam * tam;
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnMuY_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = 'y'; //Mu y

        }

        private void btnLn_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = Math.Log(tam, Math.E);
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnlg_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = Math.Log(tam, 10);
            txtNhap.Text = Convert.ToString(tam);


        }

        private void btnlog_Click(object sender, EventArgs e)
        {
            NhanPhepToan();
            pheptoan = 'l'; //log theo co so

        }

        private void btnGiaiThua_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            double t = 1;
            for (int i = 1; i <= tam; i++)
                t = t * i;
            txtNhap.Text = Convert.ToString(t);

        }

        private void btnSin_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = Math.Sin(tam*Math.PI/180);
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnCos_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = Math.Cos(tam*Math.PI/180);
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnTang_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = Math.Tan(tam*Math.PI/180);
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnInt_Click(object sender, EventArgs e)
        {
            double tam = Convert.ToDouble(txtNhap.Text.Trim());
            tam = (int)tam;
            txtNhap.Text = Convert.ToString(tam);

        }

        private void btnPI_Click(object sender, EventArgs e)
        {
            txtNhap.Text = Convert.ToString(Math.PI);

        }

        private void btnE_Click(object sender, EventArgs e)
        {
            txtNhap.Text = Convert.ToString(Math.E);

        }



    }
}